package com.ruoyi.basebill.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.basebill.DTO.QqjjtEntryInfo;
import com.ruoyi.basebill.DTO.QqjjtInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.WfzJtqqjentry;
import com.ruoyi.basebill.mapper.WfzJtqqjMapper;
import com.ruoyi.basebill.domain.WfzJtqqj;
import com.ruoyi.basebill.service.IWfzJtqqjService;

/**
 * 计提全勤奖Service业务层处理
 * 
 * @author xiefeng
 * @date 2022-12-19
 */
@Service
public class WfzJtqqjServiceImpl implements IWfzJtqqjService 
{
    @Autowired
    private WfzJtqqjMapper wfzJtqqjMapper;

    /**
     * 查询计提全勤奖
     * 
     * @param fid 计提全勤奖主键
     * @return 计提全勤奖
     */
    @Override
    public WfzJtqqj selectWfzJtqqjByFid(Long fid)
    {
        return wfzJtqqjMapper.selectWfzJtqqjByFid(fid);
    }

    /**
     * 查询计提全勤奖列表
     * 
     * @param wfzJtqqj 计提全勤奖
     * @return 计提全勤奖
     */
    @Override
    public List<WfzJtqqj> selectWfzJtqqjList(WfzJtqqj wfzJtqqj)
    {
        return wfzJtqqjMapper.selectWfzJtqqjList(wfzJtqqj);
    }

    /**
     * 新增计提全勤奖
     * 
     * @param wfzJtqqj 计提全勤奖
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWfzJtqqj(WfzJtqqj wfzJtqqj)
    {
        int rows = wfzJtqqjMapper.insertWfzJtqqj(wfzJtqqj);
        insertWfzJtqqjentry(wfzJtqqj);
        return rows;
    }

    /**
     * 修改计提全勤奖
     * 
     * @param wfzJtqqj 计提全勤奖
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWfzJtqqj(WfzJtqqj wfzJtqqj)
    {
        wfzJtqqjMapper.deleteWfzJtqqjentryByFparentid(wfzJtqqj.getFid());
        insertWfzJtqqjentry(wfzJtqqj);
        return wfzJtqqjMapper.updateWfzJtqqj(wfzJtqqj);
    }

    /**
     * 批量删除计提全勤奖
     * 
     * @param fids 需要删除的计提全勤奖主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWfzJtqqjByFids(Long[] fids)
    {
        wfzJtqqjMapper.deleteWfzJtqqjentryByFparentids(fids);
        return wfzJtqqjMapper.deleteWfzJtqqjByFids(fids);
    }

    /**
     * 删除计提全勤奖信息
     * 
     * @param fid 计提全勤奖主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWfzJtqqjByFid(Long fid)
    {
        wfzJtqqjMapper.deleteWfzJtqqjentryByFparentid(fid);
        return wfzJtqqjMapper.deleteWfzJtqqjByFid(fid);
    }

    /**
     * 新增计提全勤奖分录信息
     * 
     * @param wfzJtqqj 计提全勤奖对象
     */
    public void insertWfzJtqqjentry(WfzJtqqj wfzJtqqj)
    {
        List<WfzJtqqjentry> wfzJtqqjentryList = wfzJtqqj.getWfzJtqqjentryList();
        Long fid = wfzJtqqj.getFid();
        if (StringUtils.isNotNull(wfzJtqqjentryList))
        {
            List<WfzJtqqjentry> list = new ArrayList<WfzJtqqjentry>();
            for (WfzJtqqjentry wfzJtqqjentry : wfzJtqqjentryList)
            {
                wfzJtqqjentry.setFparentid(fid);
                list.add(wfzJtqqjentry);
            }
            if (list.size() > 0)
            {
                wfzJtqqjMapper.batchWfzJtqqjentry(list);
            }
        }
    }

    /**
     * 导入全勤奖计提
     */

    @Override
    public String importQqjjt(WfzJtqqj wfzJtqqj) {
        QqjjtInfo qqjjtInfo = new QqjjtInfo();
        String fnumber = wfzJtqqj.getFnumber();//单据编号
        String fcompany = wfzJtqqj.getFcompany();//组织（公司）
        String fperiod = wfzJtqqj.getFperiod();//薪酬期间
        String fvourtype = wfzJtqqj.getFvourtype();//凭证字

        qqjjtInfo.setNumber(fnumber);
        qqjjtInfo.setCompany(fcompany);
        qqjjtInfo.setPeriod(fperiod);
        qqjjtInfo.setVourType(fvourtype);
        qqjjtInfo.setSourcebillid(String.valueOf(wfzJtqqj.getFid()));

        List<WfzJtqqjentry> wfzJtqqjentryList = wfzJtqqj.getWfzJtqqjentryList();
        ArrayList<QqjjtEntryInfo> entry = new ArrayList<>();
        //组装分录数据
        for (int i = 0; i < wfzJtqqjentryList.size(); i++) {
            WfzJtqqjentry wfzJtqqjentry = wfzJtqqjentryList.get(i);
            String fcostnumber = wfzJtqqjentry.getFcostnumber();//成本中心
            BigDecimal fqqj = wfzJtqqjentry.getFqqj();//全勤奖
            String fcostcompany = wfzJtqqjentry.getFcostcompany();//成本中心所属财务组织
            String fjzcompany = wfzJtqqjentry.getFjzcompany();//记账公司
            String fjsfs = wfzJtqqjentry.getFjsfs();//组织间结算方式
            String fcbzxxz = wfzJtqqjentry.getFcbzxxz();//成本中心属性
            String fvourtype1 = wfzJtqqjentry.getFvourtype();//凭证字

            QqjjtEntryInfo qqjjtEntryInfo = new QqjjtEntryInfo();
            qqjjtEntryInfo.setCostCenter(fcostnumber);
            qqjjtEntryInfo.setQqj(fqqj);
            qqjjtEntryInfo.setCostcompany(fcostcompany);
            qqjjtEntryInfo.setJzcompany(fjzcompany);
            qqjjtEntryInfo.setJsfs(fjsfs);
            qqjjtEntryInfo.setCbzxxz(fcbzxxz);
            qqjjtEntryInfo.setVourType(fvourtype1);

            entry.add(qqjjtEntryInfo);
        }

        qqjjtInfo.setEntry(entry);

        //转为Json
        String json = JSONObject.toJSONString(qqjjtInfo);
        return json;
    }
}
